﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HurlBat.XEdit2D
{
    public partial class DiagramPanel : UserControl
    {
        Point currentPos = Point.Empty;
        private List<Node> nodes = new List<Node>();
        private List<Arrow> arrows = new List<Arrow>();
        private Arrow currentArrow = null;

        public DiagramPanel()
        {
            InitializeComponent();
        }

        private void pnlDiagram_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (currentArrow != null)
                {
                    currentArrow = null;
                    pnlDiagram.Invalidate();
                }
                else
                {
                    currentPos = System.Windows.Forms.Control.MousePosition;
                    contextMenuDiagram.Show(currentPos);
                }
            }
        }

        private void newImageNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImageNode n = new ImageNode(this, "image");
            n.Size = new System.Drawing.Size(150, 100);
            n.Location = pnlDiagram.PointToClient(currentPos);
            nodes.Add(n);
            pnlDiagram.Controls.Add(n);
            n.BringToFront();
            n.Focus();
            currentPos = Point.Empty;
        }

        private void newAnimationNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AnimationNode n = new AnimationNode(this, "animation");
            n.Size = new System.Drawing.Size(150, 100);
            n.Location = pnlDiagram.PointToClient(currentPos);
            nodes.Add(n);
            pnlDiagram.Controls.Add(n);
            n.BringToFront();
            n.Focus();
            currentPos = Point.Empty;
        }

        private void newCharacterNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CharacterNode n = new CharacterNode(this, "character");
            n.Size = new System.Drawing.Size(150, 100);
            n.Location = pnlDiagram.PointToClient(currentPos);
            nodes.Add(n);
            pnlDiagram.Controls.Add(n);
            n.BringToFront();
            n.Focus();
            currentPos = Point.Empty;
        }

        public void StartArrow(Node startNode, Point startMouse)
        {
            if (currentArrow != null) return;
            currentArrow = new Arrow();
            currentArrow.SetStartNode(startNode);
        }

        public void EndArrow(Node endNode)
        {
            if (currentArrow == null) return;
            if (!currentArrow.SetEndNode(endNode)) return;
            arrows.Add(currentArrow);
            currentArrow = null;
            pnlDiagram.Invalidate();
        }

        private void pnlDiagram_Paint(object sender, PaintEventArgs e)
        {
            foreach (Arrow a in arrows)
                a.Draw(e.Graphics, pnlDiagram);
            if (currentArrow != null)
                currentArrow.Draw(e.Graphics, pnlDiagram);
        }

        private void pnlDiagram_MouseMove(object sender, MouseEventArgs e)
        {
            if (currentArrow != null)
            {
                pnlDiagram.Refresh();
            }
        }
    }
}
